package fei.yuzhi.com.loveflowers.ui.ProductActivity;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import fei.yuzhi.com.loveflowers.R;
import fei.yuzhi.com.loveflowers.bean.ProductBean;

/**
 * Created by Administrator on 2016/10/10.
 */
public class ProductAdapter extends BaseAdapter {
    private List<ProductBean.DatasBean.GoodsListBean> list;
    private Context context;
    private Handler handler=new Handler();
    public ProductAdapter(List<ProductBean.DatasBean.GoodsListBean> list,Context context){
        this.context=context;
        this.list=list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
         ViewHolder holder;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.listview_product_adapter,null);
            holder=new ViewHolder();
            holder.iv_productadapter= (ImageView) convertView.findViewById(R.id.iv_productadapter);
            holder.tv_productadaptertitle= (TextView) convertView.findViewById(R.id.tv_productadaptertitle);
            holder.tv_productadapterprice= (TextView) convertView.findViewById(R.id.tv_productadapterprice);
            holder.tv_productadaptersale= (TextView) convertView.findViewById(R.id.tv_productadaptersale);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        Log.i("33333","----"+list.get(1).getGoodsName());
        holder.tv_productadaptertitle.setText(list.get(position).getGoodsName());
        holder.tv_productadapterprice.setText("￥ "+list.get(position).getGoodsPrice());
        holder.tv_productadaptersale.setText("已售 "+list.get(position).getGoodsSalenum());
        final String str=list.get(position).getGoodsImageUrl();
        final ViewHolder viewHolder=holder;
        new Thread(new Runnable() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Picasso.with(context).load(str).into(viewHolder.iv_productadapter);
                    }
                });
            }
        }).start();

        return convertView;
    }
    static class ViewHolder{
        ImageView iv_productadapter;
        TextView tv_productadaptertitle,tv_productadapterprice,tv_productadaptersale;
    }
}
